package com.moore.dto;

import java.util.List;

public class ResponseData {
    private int code;   //状态码	：自己定义	200成功	500错误
    private String message; //描述
    private Object data;    //数据    对象或ArrList
    private int total;  //分页总行数

    public ResponseData() {
    }

    public ResponseData(int code, String message, Object data, int total) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.total = total;
    }
    //返回success
    public static ResponseData ok(Object data){
        return new ResponseData(0,"success",data,0);
    }
    //返回fail
    public static ResponseData error(Object data){
        return new ResponseData(0,"fail",data,0);
    }
    //返回list
    public <T> ResponseData returnList(List<T> list , int count) {
        ResponseData responseData = new ResponseData();
        responseData.setData(list);
        responseData.setTotal(count);
        responseData.setCode(200);
        responseData.setMessage(count > 0 ? "success" : "fail");

        return responseData;
    }
//  返回（插入、删除、修改等不需传回数据的请求）返回是否成功
    public ResponseData returnState(int count) {
        ResponseData responseData = new ResponseData();
        responseData.setData(count > 0 ? "ok" : "no");
        responseData.setTotal(count);
        responseData.setCode(count > 0 ? 200 : 400);
        responseData.setMessage(count > 0 ? "success" : "fail");
        return responseData;
    }
//    返回数值
    public ResponseData returnData(int count) {
        ResponseData responseData = new ResponseData();
        responseData.setData(count);
        responseData.setTotal(1);
        responseData.setCode(count > 0 ? 200 : 400);
        responseData.setMessage(count > 0 ? "success" : "fail");
        return responseData;
    }
    //返回对象
    public ResponseData returnObject(Object object){
        ResponseData responseData = new ResponseData();
        responseData.setData(object);
        responseData.setTotal(1);
        responseData.setCode(object!=null ? 200 : 400);
        responseData.setMessage(object!=null ? "success" : "fail");
        return responseData;
    }

    //返回fail
    //(加提示消息)
    public static ResponseData error(String message){
        return new ResponseData(0,message,null,0);
    }
    //返回list(加状态码)
    //(加提示消息)
    public static<T> ResponseData returnList(String message, List<T> list , int count) {
        ResponseData responseData = new ResponseData();
        responseData.setData(list);
        responseData.setTotal(count);
        responseData.setCode(200);
        responseData.setMessage(message);

        return responseData;
    }
    //  返回（插入、删除、修改等不需传回数据的请求）返回是否成功
    //(加提示消息)
    public static ResponseData returnState(String message, int count) {
        ResponseData responseData = new ResponseData();
        responseData.setData(count > 0 ? "ok" : "no");
        responseData.setTotal(count);
        responseData.setCode(count > 0 ? 200 : 400);
        responseData.setMessage(message);
        return responseData;
    }
    //    返回数值
    //(加提示消息)
    public static ResponseData returnData(String message, int count) {
        ResponseData responseData = new ResponseData();
        responseData.setData(count);
        responseData.setTotal(1);
        responseData.setCode(count > 0 ? 200 : 400);
        responseData.setMessage(message);
        return responseData;
    }
    //返回对象
    //(加提示消息)
    public static ResponseData returnObject(String message, Object object){
        ResponseData responseData = new ResponseData();
        responseData.setData(object);
        responseData.setTotal(1);
        responseData.setCode(object!=null ? 200 : 400);
        responseData.setMessage(message);
        return responseData;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    @Override
    public String toString() {
        return "ResponseData{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", total=" + total +
                '}';
    }
}
